package com.ali.music.navigator.backstack;

import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;

/**
 * @author hongkai.qian
 * @version 1.0.0
 * @since 15-12-07
 */
public class AbstractActivity extends FragmentActivity {

    private static final String TAG = "AbstractActivity";

    protected FragmentActivityBackHelper mFragmentActivityBackHelper = new FragmentActivityBackHelper(this);

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        boolean result = false;
        if (KeyEvent.KEYCODE_BACK == keyCode) {
            result = mFragmentActivityBackHelper.onBackPressed();
        } else {
            result = mFragmentActivityBackHelper.onKeyDown(keyCode, event);
        }

        if (!result) {
            return super.onKeyDown(keyCode, event);
        } else {
            return result;
        }
    }

    /**
     * 设置弹出fragment的属性
     *
     * @param containerViewRes containerViewRes
     * @param enterAnimRes     enterAnimRes
     * @param popExitAnimRes   popExitAnimRes
     */
    public void setLaunchFragmentAttr(int containerViewRes, int enterAnimRes, int popExitAnimRes) {
        mFragmentActivityBackHelper.setLaunchFragmentAttr(containerViewRes, enterAnimRes, popExitAnimRes);
    }

    public final void launchRootFragment(AbstractFragment fragment, String tag) {
        mFragmentActivityBackHelper.launchRootFragment(fragment, tag);
    }

    public AbstractFragment getRootFragment() {
        return (AbstractFragment) mFragmentActivityBackHelper.getRootFragment();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mFragmentActivityBackHelper.clearFragmentBackStack();
    }

    public FragmentActivityBackHelper getFragmentActivityBackHelper() {
        return mFragmentActivityBackHelper;
    }

    /**
     * Fragment回退栈是否为空
     *
     * @return true/false
     */
    protected final boolean isFragmentBackStackEmpty() {
        return mFragmentActivityBackHelper.isFragmentBackStackEmpty();
    }

    /**
     * 启动Fragment，这样启动的Fragment由这个Fragment所在的Activity管理，会加入backStack中
     *
     * @param fragment fragment
     * @deprecated use {@link com.ali.music.navigator.Navigator.Builder} API
     */
    public final void launchFragment(AbstractFragment fragment) {
        mFragmentActivityBackHelper.launchFragment(fragment);
    }

    /**
     * 启动Fragment，会加入到backstack
     *
     * @param fragment       fragment
     * @param containId      添加fragment的layout id, 0代表使用默认的
     * @param enterAnimRes   进入动画
     * @param popExitAnimRes 退出动画
     * @param launchFlag     flag
     * @deprecated use {@link com.ali.music.navigator.Navigator.Builder} API
     */
    public final void launchFragment(AbstractFragment fragment, int containId, int enterAnimRes, int popExitAnimRes, int launchFlag) {
        mFragmentActivityBackHelper.launchFragment(fragment, containId, enterAnimRes, popExitAnimRes, launchFlag);
    }

    /**
     * 弹出顶层Fragment
     */
    public final void popTopFragment() {
        mFragmentActivityBackHelper.finishTopFragment();
    }

    /**
     * 设置最底层的Fragment，这个fragment不会被pop
     *
     * @deprecated use {@link #launchRootFragment} instead
     *
     * @param fragment fragment
     */
    @Deprecated
    protected final void setPrimaryFragment(AbstractFragment fragment) {
        mFragmentActivityBackHelper.launchRootFragment(fragment, "");
    }

    /**
     * @deprecated use {@link com.ali.music.navigator.Navigator.Builder} API
     */
    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
    }

    /**
     * 获取最底层的Fragment
     *
     * @deprecated use {@link #getRootFragment} instead
     *
     * @return
     */
    @Deprecated
    protected AbstractFragment getPrimaryFragment() {
        return (AbstractFragment) mFragmentActivityBackHelper.getRootFragment();
    }

    /**
     * @return BaseFragment
     */
    public AbstractFragment getTopFragment() {
        return (AbstractFragment) mFragmentActivityBackHelper.getTopFragment();
    }

    public FragmentBackStackManager getFragmentBackStackManager() {
        return mFragmentActivityBackHelper.getFragmentBackStackManager();
    }

    @Override
    public void onBackPressed() {
        if (!mFragmentActivityBackHelper.onBackPressed()) {
            super.onBackPressed();
        }
    }

    public Params getParams() {
        return IntentParam.from(getIntent());
    }
}
